package com.search.poi.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.search.poi.domain.User;

import net.sf.jxls.transformer.XLSTransformer;

public class ExcelUtil {

	public static void writeExcel(List<User> list) {
		File upload = new File("D:/SearchPOI");
		String path = upload.getPath();
        //将结果放入这个list中
		Map<String, Object> beans = new HashMap<String, Object>();
		Date date = new Date();
		SimpleDateFormat simpl = new SimpleDateFormat("yyyyMMddHHmmss");
		String currntTime = simpl.format(date);
		path += "/searchpoi.xls";
		//导出列表名
		String fileName = "水泥门店"+currntTime;
		beans.put("values", list);
		try {
	        //文件名称统一编码格式
			fileName = URLEncoder.encode(fileName, "utf-8");
			//生成的导出文件
			File destFile = File.createTempFile(fileName, ".xls");
			//transformer转到Excel
			XLSTransformer transformer = new XLSTransformer();
            //将数据添加到模版中生成新的文件
			transformer.transformXLS(path, beans, destFile.getAbsolutePath());
            //将文件输入
			InputStream inputStream = new FileInputStream(destFile);
            byte[] b = new byte[1024];
            String filePath = upload + "/"+new Date().getTime() + ".xls";
			FileOutputStream outputStream = new FileOutputStream(new File(filePath));
			while (inputStream.read(b) != -1) {
				outputStream.write(b);
			}
			outputStream.close();
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
